﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace AFanDa.DAL
{
    public class SqlRepository<T> : IRepository<T> where T : class, new()
    {
        DbContext _dbContext = null;

        /// <summary>
        /// 实例化对象 自动调用
        /// </summary>
        public SqlRepository()
        {
            _dbContext = DbContextFactory.CreateDbContext();
        }

        /// <summary>
        /// 添加单个
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            _dbContext.Set<T>().Add(t);
        }
        /// <summary>
        /// 添加多个
        /// </summary>
        /// <param name="Ie"></param>
        public void AddRange(IEnumerable<T> Ie)
        {
            _dbContext.Set<T>().AddRange(Ie);

        }
        /// <summary>
        /// 删除单个
        /// </summary>
        /// <param name="t"></param>
        public void Delete(T t)
        {
            _dbContext.Set<T>().Remove(t);

        }
        /// <summary>
        /// 删除多个
        /// </summary>
        /// <param name="Ie"></param>
        public void DeleteRange(IEnumerable<T> Ie)
        {
            _dbContext.Set<T>().RemoveRange(Ie);

        }
        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>> where = null)
        {
            var iq = (IQueryable<T>)_dbContext.Set<T>();
            if (where != null)
            {
                iq = iq.Where(where);
            }
            return iq;
        }
        /// <summary>
        /// 查询所有 在排序
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="order">排序条件</param>
        /// <param name="isAsc">升序或者降序</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>> where = null)
        {
            var iq = this.QueryAll(where);
            if (order != null)
                return isAsc ? iq.OrderBy(order) : iq.OrderByDescending(order);
            return iq;

        }
        /// <summary>
        /// 查询所有 排序后分页
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="total">总数据数量</param>
        /// <param name="skip">第几页</param>
        /// <param name="take">跳过多少条</param>
        /// <param name="order">排序条件</param>
        /// <param name="isAsc">升序或者降序</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public IQueryable<T> QueryAll<type>(out int total, int? skip, int? take, Expression<Func<T, type>> order, bool isAsc, Expression<Func<T, bool>> where = null)
        {
            var iq = this.QueryAll(order, isAsc, where);
            total = iq.Count();
            return iq.Skip(Convert.ToInt32(skip)).Take(Convert.ToInt32(take));

        }



        /// <summary>
        /// 保存修改
        /// </summary>
        /// <returns></returns>
        public int SaveChange()
        {
            return _dbContext.SaveChanges();
        }
        /// <summary>
        /// 修改单个
        /// </summary>
        /// <param name="t"></param>
        public void Update(T t)
        {
            _dbContext.Entry<T>(t).State = EntityState.Modified;
        }
        /// <summary>
        /// 修改多个
        /// </summary>
        /// <param name="Ie"></param>
        public void UpdateRange(IEnumerable<T> Ie)
        {
            foreach (var item in Ie)
            {
                _dbContext.Entry<T>(item).State = EntityState.Modified;

            }
        }
    }

}
